/**
 * Class: Midica
 *
 * Purpose: 
 *
 * Created: Feb 16, 2010 @ 11:32:24 PM
 * @author Ken Scott
 */
package org.openfantasia.musica;

import org.openfantasia.util.DateTime;

import javax.sound.midi.*;
import java.util.ArrayList;
import java.util.List;

public class Midica {

	protected List<Synthesizer> synthesizers = new ArrayList<Synthesizer>();


	public static void main(String[] args) {
		int[] ids = new int[]{12, 86, 90, 48};
		Midica midi = new Midica(ids);

		for(int i = 0; i < ids.length; i++) {
			for(int tone = 50; tone < 62; tone++) {
				midi.play(i, 0, tone, 70);
				DateTime.waitMilliseconds(333);
			}
		}
	}

	public Midica(int... instrumentId) {
		for (int i = 0; i < instrumentId.length; i++) {
			try {
				Synthesizer synth = MidiSystem.getSynthesizer();
				synth.open();
				Soundbank sb = synth.getDefaultSoundbank();
				synth.loadAllInstruments(sb);
				synth.loadInstrument(synth.getAvailableInstruments()[instrumentId[i]]);
				synthesizers.add(synth);
			}
			catch (MidiUnavailableException e) {
				e.printStackTrace();
			}
		}
	}


	public void play(int id, int channel, int tone, int velocity) {
		Synthesizer synth = getSynthesizer(id);

		// Play the note Middle C (60) moderately loud
		// (velocity = 93)on channel 4 (zero-based).
		try {
			ShortMessage myMsg = new ShortMessage();
			myMsg.setMessage(ShortMessage.NOTE_ON, channel, tone, velocity);
			synth.getReceiver().send(myMsg, -1);
		}
		catch (InvalidMidiDataException e) {
			e.printStackTrace();
		}
		catch (MidiUnavailableException e) {
			e.printStackTrace();
		}
	}


	public Synthesizer getSynthesizer(int id) {
		return synthesizers.get(id);
	}

	/**
	 0 Piano
	 1 Bright Piano
	 2 Electric Grand
	 3 Honky Tonk Piano
	 4 Electric Piano 1
	 5 Electric Piano 2
	 6 Harpsichord
	 7 Clavinet
	 8 Celesta
	 9 Glockenspiel
	 10 Music Box
	 11 Vibraphone
	 12 Marimba
	 13 Xylophone
	 14 Tubular Bell
	 15 Dulcimer
	 16 Hammond Organ
	 17 Perc Organ
	 18 Rock Organ
	 19 Church Organ
	 20 Reed Organ
	 21 Accordion
	 22 Harmonica
	 23 Tango Accordion
	 24 Nylon Str Guitar
	 25 Steel String Guitar
	 26 Jazz Electric Gtr
	 27 Clean Guitar
	 28 Muted Guitar
	 29 Overdrive Guitar
	 30 Distortion Guitar
	 31 Guitar Harmonics
	 32 Acoustic Bass
	 33 Fingered Bass
	 34 Picked Bass
	 35 Fretless Bass
	 36 Slap Bass 1
	 37 Slap Bass 2
	 38 Syn Bass 1
	 39 Syn Bass 2
	 40 Violin
	 41 Viola
	 42 Cello
	 43 Contrabass
	 44 Tremolo Strings
	 45 Pizzicato Strings
	 46 Orchestral Harp
	 47 Timpani
	 48 Ensemble Strings
	 49 Slow Strings
	 50 Synth Strings 1
	 51 Synth Strings 2
	 52 Choir Aahs
	 53 Voice Oohs
	 54 Syn Choir
	 55 Orchestra Hit
	 56 Trumpet
	 57 Trombone
	 58 Tuba
	 59 Muted Trumpet
	 60 French Horn
	 61 Brass Ensemble
	 62 Syn Brass 1
	 63 Syn Brass 2
	 64 Soprano Sax
	 65 Alto Sax
	 66 Tenor Sax
	 67 Baritone Sax
	 68 Oboe
	 69 English Horn
	 70 Bassoon
	 71 Clarinet
	 72 Piccolo
	 73 Flute
	 74 Recorder
	 75 Pan Flute
	 76 Bottle Blow
	 77 Shakuhachi
	 78 Whistle
	 79 Ocarina
	 80 Syn Square Wave
	 81 Syn Saw Wave
	 82 Syn Calliope
	 83 Syn Chiff
	 84 Syn Charang
	 85 Syn Voice
	 86 Syn Fifths Saw
	 87 Syn Brass and Lead
	 88 Fantasia
	 89 Warm Pad
	 90 Polysynth
	 91 Space Vox
	 92 Bowed Glass
	 93 Metal Pad
	 94 Halo Pad
	 95 Sweep Pad
	 96 Ice Rain
	 97 Soundtrack
	 98 Crystal
	 99 Atmosphere
	 100 Brightness
	 101 Goblins
	 102 Echo Drops
	 103 Sci Fi
	 104 Sitar
	 105 Banjo
	 106 Shamisen
	 107 Koto
	 108 Kalimba
	 109 Bag Pipe
	 110 Fiddle
	 111 Shanai
	 112 Tinkle Bell
	 113 Agogo
	 114 Steel Drums
	 115 Woodblock
	 116 Taiko Drum
	 117 Melodic Tom
	 118 Syn Drum
	 119 Reverse Cymbal
	 120 Guitar Fret Noise
	 121 Breath Noise
	 122 Seashore
	 123 Bird
	 124 Telephone
	 125 Helicopter
	 126 Applause
	 127 Gunshot
	 128 Hi-Q
	 129 Slap
	 130 Scratch Push
	 131 Scratch Pull
	 132 Sticks
	 133 Square Click
	 134 Metronome Click
	 135 Metronome Bell
	 136 Low Kick
	 137 Kick
	 138 Rimclik
	 139 Acoustic Snare
	 140 Handclap
	 141 Electric Snare
	 142 Tom Low
	 143 Closed Hi-Hat
	 144 Tom Hi
	 145 Pedal Hi-Hat
	 146 Tom Hi
	 147 Open Hi-Hat
	 148 Low Mid Tom
	 149 High Mid Tom
	 150 Crash Cymbal 1
	 151 High Tom
	 152 Ride Cymbal 1
	 153 Chinese Cymbal
	 154 Ride Bell
	 155 Tambourine
	 156 Splash Cymbal
	 157 Cowbell
	 158 Crash Cymbal 2
	 159 Vibraslap
	 160 Ride Cymbal 2
	 161 High Bongo
	 162 Low Bongo
	 163 Mute High Conga
	 164 Open High Conga
	 165 Low Conga
	 166 High Timbale
	 167 Low Timbale
	 168 High Agogo
	 169 Low Agogo
	 170 Cabasa
	 171 Maracas
	 172 Short Whistle
	 173 Long Whistle
	 174 Short Guiro
	 175 Long Guiro
	 176 Claves
	 177 High Wood Block
	 178 Low Wood Block
	 179 Mute Cuica
	 180 Open Cuica
	 181 Close Triangle
	 182 Open Triangle
	 183 Shaker
	 184 Jingle Bell
	 185 Bell Tree
	 186 Castanets
	 187 Mute Surdo
	 188 Open Surdo
	 189 Brush Snare High
	 190 Brush Snare Low
	 191 Soft Piano
	 192 Reflection Piano
	 193 Flange Piano
	 194 Octopus Piano
	 195 Flange Electric Piano
	 196 Tremolo Electric Piano
	 197 Chorus Harpsichord
	 198 Ondioline
	 199 Curious Chime
	 200 Swirly Bell
	 201 Echo Bell
	 202 Vibraphone 2
	 203 Soft Marimba
	 204 Percolator
	 205 Reverse Bell
	 206 Percussive Piano
	 207 Rot. Speaker Organ 1
	 208 Bright Perc Organ
	 209 Growl Organ
	 210 Chorus Church Organ
	 211 Sawteeth
	 212 Expressive Accordion
	 213 Corny Harmonica
	 214 Squeeze Box
	 215 Acoustic Bass Gtr
	 216 Mute Guitar 2
	 217 Chorus Jazz Guitar
	 218 Tremolo Guitar
	 219 Echo Mute Bass
	 220 Grungey Guitar
	 221 Flange Guitar
	 222 Flange Guitar Stab
	 223 Dark Syn Bass
	 224 Jungle Bass
	 225 Jungle Bass 2
	 226 Chorus Bass
	 227 Synth Slap Bass
	 228 Lowrider
	 229 OB Bass
	 230 OB Bass 2
	 231 Synth Violin
	 232 Robot Threat
	 233 Formula 1
	 234 Deep Bass Stab
	 235 Trill Strings
	 236 Cello Stroke
	 237 VibHarp
	 238 Tuned Drum
	 239 sfz Strings
	 240 Moving Strings
	 241 Heads of Space
	 242 Slow Synth Strings
	 243 Slow Choir Aahs
	 244 Pulsing Voices
	 245 Cloudscapes
	 246 Hype Orch Hit
	 247 Accent Trumpet
	 248 Accent Trombone
	 249 Mellow Trombone
	 250 Muted Trumpet 2
	 251 French Horn Swell
	 252 Brass Ensemble 2
	 253 Syn Brass 3
	 254 Syn Brass 4
	 255 Lyrical Sax
	 256 Accent Sax
	 257 Slow Sax
	 258 Staccato Sax
	 259 Lyrical Oboe
	 260 Soft Lead Synth
	 261 Staccato Bassoon
	 262 Smooth Clarinet
	 263 Accent Piccolo
	 264 Lyrical Flute
	 265 Mello Breath
	 266 Peruvian
	 267 Breath Echoes
	 268 Apparition
	 269 Humorous Whistle
	 270 Strobes
	 271 Teletronic
	 272 5th Pulse
	 273 Moon Jelly
	 274 Ricochet Pad
	 275 Rock Radiation
	 276 Solo Vox 2
	 277 5th Saw Drone
	 278 Lead Synth 2
	 279 Fantasy Bells
	 280 Slow Warm Pad
	 281 Polysynth 2
	 282 50's Sci-Fi
	 283 Watery Glass
	 284 UFOs
	 285 Soprano
	 286 Refractions
	 287 Comet Tails
	 288 Soundtrack 2
	 289 Friendly Data
	 290 Analog Sequence
	 291 Luminous Voice
	 292 Goblins 2
	 293 Tick Blok
	 294 Droplet
	 295 Droplet 2
	 296 Video Game 1
	 297 Video Game 2
	 298 Digi-Dodo
	 299 Fairy Godmother
	 300 Alarm
	 301 Hoverbug
	 302 Crickets
	 303 Beatnik
	 304 Teletype
	 305 Carriage Return
	 306 Typewriter Key
	 307 Cash Register
	 308 Thru Phone Chirp
	 309 Whipped
	 310 Pop Click
	 311 Metal Chirp
	 312 Poing
	 313 Metal Spray
	 314 FlyBy 1
	 315 FlyBy 2
	 316 Cosmic Ray
	 317 SampHold
	 318 SampHold 2
	 319 tablaesque_lo
	 320 tablaesque_hi
	 321 nine_inch_kick
	 322 hippishake
	 323 ScienceTom
	 324 click
	 325 rvs cymb pan long
	 326 rvs cymb pan med
	 327 rvs cymb pan shrt
	 328 woodstick
	 329 electrobrite
	 330 SloLaser
	 331 ufo-by
	 332 air snare
	 333 weird snare
	 334 flange snare
	 335 ping drum
	 336 itchy-scratch
	 337 itchy-scratch2
	 338 hi_metallic_snare
	 339 noise echo
	 340 buzzy hit
	 341 rez_hat
	 342 bucket hit
	 343 icecube
	 344 explosive kick
	 345 mutant cowbell
	 346 Temple Bell
	 347 Hi Temple Gong
	 348 Thuddy Kick
	 349 LoBell
	 350 sinechirp
	 351 hollow_metal_hit
	 352 woodblock
	 353 reverse cymbal
	 354 tablaesque_slap
	 355 sonarblip
	 356 sinehit
	 357 sineblock
	 358 Krelltone
	 359 sine_kick
	 360 hi-tone*
	 361 hi-click
	 362 Gate-tone
	 363 chem-tone
	 364 dub_kick
	 365 tablaesque_lo
	 366 tablaesque_hi
	 367 nine_inch_kick
	 368 hippishake
	 369 ScienceTom
	 370 click
	 371 rvs cymb pan long
	 372 rvs cymb pan med
	 373 rvs cymb pan shrt
	 374 woodstick
	 375 electrobrite
	 376 SloLaser
	 377 ufo-by
	 378 air snare
	 379 weird snare
	 380 flange snare
	 381 ping drum
	 382 itchy-scratch
	 383 itchy-scratch2
	 384 hi_metallic_snare
	 385 noise echo
	 386 buzzy hit
	 387 rez_hat
	 388 bucket hit
	 389 icecube
	 390 explosive kick
	 391 mutant cowbell
	 392 Temple Bell
	 393 Hi Temple Gong
	 394 Thuddy Kick
	 395 LoBell
	 396 sinechirp
	 397 hollow_metal_hit
	 398 woodblock
	 399 reverse cymbal
	 400 tablaesque_slap
	 401 sonarblip
	 402 sinehit
	 403 sineblock
	 404 Krelltone
	 405 sine_kick
	 406 hi-tone*
	 407 hi-click
	 408 Gate-tone
	 409 chem-tone
	 410 dub_kick
	 */
}
